import { createRouter, createWebHistory } from 'vue-router'

import HomeView from '../views/HomeView.vue'
import login from '../views/login.vue'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log('全局前置守卫：', to.meta.requiresAuth, from);
  // 例如，检查用户是否已登录
  if (to.meta.requiresAuth) {
    console.log('需要认证的路由');
    // 用户未登录，重定向到登录页面
    next({ name: 'login' });
  } else {
    // 允许访问
    console.log('允许访问');
    next();
  }
});

// 全局解析守卫
router.beforeResolve((to, from, next) => {
  // 在导航被确认之前，同时所有组件内守卫和异步路由组件被解析之后，解析守卫就被调用。
  // 例如，如果你想通过全局方式解析数据或执行某些操作，这里可以做到。
  next();
});


export default router
